package com.tj.shopping.service.impl;

import com.tj.shopping.entity.Admin;
import com.tj.shopping.entity.User;
import com.tj.shopping.enums.ResultEnum;
import com.tj.shopping.mapper.AdminMapper;
import com.tj.shopping.mapper.UserMapper;
import com.tj.shopping.service.IAdminService;
import com.tj.shopping.util.MybatisUtil;
import com.tj.shopping.util.StringUtils;
import com.tj.shopping.vo.ResultVO;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminServiceImpl implements IAdminService {

    private AdminMapper mapper;


    @Override
    public Map<String, Object> findbyid(Map<String, Object> map) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(AdminMapper.class);
        List<Admin> list = mapper.findbyid(map);

        Map<String,Object> m = new HashMap<>();
        if (null == list || list.isEmpty()){
            m.put("code",600);
            m.put("msg","暂无数据");
        } else {
            m.put("code",200);
            m.put("msg","成功");
            m.put("data",list);

        }
        MybatisUtil.close(session);
        return m;

    }

    @Override
    public Map<String, Object> findall() {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(AdminMapper.class);
        List<Admin> list1 = mapper.findall();

        Map<String,Object> map2 = new HashMap<>();
        if (null == list1 || list1.isEmpty()){
            map2.put("code",600);
            map2.put("msg","暂无数据");
        } else {
            map2.put("code",200);
            map2.put("msg","成功");
            map2.put("data",list1);

        }
        MybatisUtil.close(session);
        return map2;
    }

    @Override
    public int update(Admin admin) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(AdminMapper.class);

        if (admin.getAstate()==1){
            admin.setAstate(0);
        }else {
            admin.setAstate(1);
        }

        return mapper.update(admin);
    }



//    @Override
//    public Map<String, Object> findall(Map<String, Object> map) {
//        SqlSession session = MybatisUtil.getSession(true);
//        mapper = session.getMapper(AdminMapper.class);
//        List<Admin> list = mapper.findall(map);
//
//        Map<String,Object> map2 =
//        if (null == list || list.isEmpty()){
//            map2.put("code",600);
//            map2.put("msg","暂无数据");
//        } else {
//            map2.put("code",200);
//            map2.put("msg","成功");
//            map2.put("data",list);
//
//        }
//        MybatisUtil.close(session);
//        return  map2;
//
//    }


    @Override
    public int add(Admin admin) {
        SqlSession session = MybatisUtil.getSession(true);
        mapper = session.getMapper(AdminMapper.class);
        return mapper.add(admin);
    }

    @Override
    public Admin login(String aname, String apwd) {
        //判断用户名或密码是否为空
        if (StringUtils.isNull(aname)|| StringUtils.isNull(apwd)){
            return null;
        }
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(AdminMapper.class);
        Admin admin = mapper.login(aname,apwd);
        return admin;
    }
}
